def generate(n):
    # 初始化杨辉三角
    triangle=[]

    #生成杨辉三角
    for i in range(n):
        row=[1]*(i+1)
        triangle.append(row)
        for j in range(1,i):
            row[j]=triangle[i-1][j-1]+triangle[i-1][j]
    return triangle

def display(triangle):
    # 计算每个数字的最大位数
    max_digits = len(str(max(triangle[-1])))

    for row in triangle:

        row_str = " ".join(str(num).center(max_digits) for num in row)
        # 居中对齐输出
        print(row_str.center(len(triangle) * max_digits * 2))
display(generate(6))
